const getCurrentAllDate = require('../../utils/util')
const App = getApp();

Page({
  data: {
    ratingValue: 0,
    reviewPlaceholder: '大家都在问：剧情怎么样、画面好吗、演技如何？',
    currentDate: '',
    reviewText: '' ,
    Token:''
  },

 onLoad(){
  const userToken = wx.getStorageSync('userToken'); 
  this.setData({
    Token:userToken
  });
   this.setData({
    currentDate: getCurrentAllDate.getCurrentAllDate(),
   })
 },
 //设置评价星级
  setRating(e) {
    this.setData({
      ratingValue: e.currentTarget.dataset.index + 1
    });
  },
  // 更新用户输入的评论 
  onReviewInput(e) {  
    this.setData({  
      reviewText: e.detail.value   
    });  
  },  
  // 将评价数据发送到后端进行保存
  publishReview() {
    if (this.data.ratingValue > 0 && this.data.reviewText !== '') {
      const evaluate={
        avatarUrl:App.globalData.avatar_url,
        username:App.globalData.nickname,
        comment:this.data.reviewText,
        date:this.data.currentDate,
        location:"福建"
      };
      wx.request({
        url: App.globalData.backend_add + 'api/insertComment',
        method: 'POST',
        header: {
          'Authorization': this.data.Token,
          'Content-Type': 'application/json'
        },
        data: JSON.stringify(evaluate),
        success: function (res) {
          console.log('上传成功：', res);
        },
        fail: function (err) {
          console.error('上传失败：', err);
        }
      });
      wx.showToast({
        title: '评价发布成功',
        icon: 'success',
        duration: 2000
      });
      // 返回到上一页面
      wx.navigateBack();
    } else {
      wx.showToast({
        title: '请完成评价后再发布',
        icon: 'none',
        duration: 2000
      });
    }
  }
});